package edu.unc.comp410.colemangeer.model.constraints;

import org.apache.log4j.Logger;



/**
 * Objects may be optional or required. A <CODE>null<CODE/> value 
 * is not allowed for required fields. 
 */
public class ObjectConstraint extends Constraint {	
	
		// Data members.
	private Boolean dataOptional;	// Is the data field required or optional?
	private Class dataType;		// Required type of data in this field.
	private static Logger log = Logger.getLogger( ObjectConstraint.class );

	public ObjectConstraint( String theName, Boolean theOptional,
		Class theDataType ) {
		
		super( theName );
		if( theDataType.isPrimitive() )
			throw new IllegalArgumentException( "Primitive datatype not allowed" );
		dataOptional = theOptional;
		dataType = theDataType;
	}
	
	public Class getDataType() { return dataType; }
	
	public Boolean isOptional() { return dataOptional; }
	
	/**
	 * Is the data type of theObject an allowable type to be assigned
	 * to this object?
	 */
	@Override public boolean isLegal( Object theObject ) {
		boolean validObject = true;
		if( null == theObject ) {
			if( ! dataOptional ) validObject = false;
		}

		else if( ! dataType.isAssignableFrom( theObject.getClass() ) )
			validObject = false;
		return validObject;
	}

}
